
AIDS.Bans = {}

if CLIENT then
	
	local function BansSynch( handler, id, encoded, decoded )
	
		AIDS.Bans = decoded
	
	end
	datastream.Hook( "BansSynch", BansSynch )
	
	local function RemoveBan( id )
	
		RunConsoleCommand( "aids_remove_ban", id )
	
	end
	
	local function AddBan( id, reason )
	
		RunConsoleCommand( "aids_add_ban", id, reason )
	
	end
	
	local function MakePanel()

		local panel = vgui.Create( "DPanel" )
		panel.InvalidateLayout = function()
		
			if panel.List then
			
				panel.List:StretchToParent( 0, 35, 0, 0 )
				
				panel.Box1:SetPos( 0, 7 )
				panel.Box2:SetPos( 165, 7 )
				panel.Butan:SetPos( 330, 5 )
				
			end
		
		end
		
		local textbox1 = vgui.Create( "DTextEntry", panel )
		textbox1:SetWide( 150 )
		textbox1:SetText( "Steam ID" )
		panel.Box1 = textbox1
		
		local textbox2 = vgui.Create( "DTextEntry", panel )
		textbox2:SetWide( 150 )
		textbox2:SetText( "Name + Ban Reason" )
		panel.Box2 = textbox2
		
		local butan = vgui.Create( "DButton", panel )
		butan:SetText( "Add Ban" )
		butan:SetWide( 100 )
		butan.DoClick = function()
		
			butan.List:AddLine( textbox1:GetText(), textbox2:GetText() )
			AddBan( textbox1:GetText(), textbox2:GetText() )
			surface.PlaySound( Sound( "buttons/button14.wav" ) )
		
		end
		panel.Butan = butan
		
		local list = vgui.Create( "DListView", panel )
		panel.List = list
		butan.List = list
		
		local col1 = list:AddColumn( "Steam ID" )
		local col2 = list:AddColumn( "Reason" )
		col1:SetMinWidth( 150 )
		col1:SetMaxWidth( 150 )
		
		return panel
		
	end
	
	local function PopulateList( panel )
	
		local list = panel.List
		
		if not list then return end
	
		for k,v in pairs( AIDS.Bans ) do
		
			list:AddLine( v[1], v[2] )
		
		end
	
		list.OnRowRightClick = function( line, id )
	
			local menu = vgui.Create( "DMenu", list )
			menu:AddOption( "Cancel" )
			menu:AddOption( "Undo Ban", function() 
				
				list:RemoveLine( id ) 
				table.remove( AIDS.Bans, id ) 
				
				RemoveBan( id )
				
				surface.PlaySound( Sound( "buttons/button14.wav" ) )
				
			end )
	
		end
		
	end
	
	AIDS.AddMenuTab( "Bans", "gui/silkicons/shield", "Manage the list of banned players.", MakePanel, PopulateList )

else
	
	local function BanAuth( ply )
	
		local id = ply:SteamID()
		
		for k,v in pairs( AIDS.Bans ) do
		
			if v[1] == id then
			
				ply:Kick( "Banned: " .. v[2] )
				
				return
			
			end
		
		end
	
	end
	
	AIDS.Hook( "PlayerAuthed", "bans", BanAuth )
	
	local function BanInitPly( ply )
	
		datastream.StreamToClients( { ply }, "BansSynch", AIDS.Bans )
	
	end
	
	AIDS.Hook( "PlayerInitialSpawn", "bans", BanInitPly )
	
	local function BanInit()
	
		local str = file.Read( "AIDS/bans.txt" )
		local tbl = string.Explode( "\n", ( str or "" ) )
		
		for k,v in pairs( tbl ) do
		
			table.insert( AIDS.Bans, string.Explode( "@", v ) )
		
		end
	
	end
	
	AIDS.Hook( "Initialize", "bans", BanInit )
	
	local function WriteToFile()
	
		local str = ""
	
		for k,v in pairs( AIDS.Bans ) do
		
			str = str .. "\n" .. v[1] .. "@" .. v[2]
		
		end
		
		str = string.Right( str, string.len( str ) - 1 )
		
		file.Write( "AIDS/bans.txt", str )
		
		print( "[AIDS] Updated banned player list." )
	
	end
	
	local function Synch()
	
		local tbl = {}
		
		for k,v in pairs( player.GetAll() ) do
		
			if v:IsAdmin() or v:IsSuperAdmin() then
			
				table.insert( tbl, v )
			
			end
		
		end
		
		if not tbl[1] then return end
	
		datastream.StreamToClients( tbl, "BansSynch", AIDS.Bans )
	
	end
	
	local function RemoveBan( ply, cmd, args )
	
		if not ( ply:IsAdmin() or ply:IsSuperAdmin() ) or not args[1] then return end
		
		table.remove( AIDS.Bans, tonumber( args[1] ) )
		
		WriteToFile()
		Synch()
	
	end
	concommand.Add( "aids_remove_ban", RemoveBan )
	
	local function AddBan( ply, cmd, args )
	
		if not ( ply:IsAdmin() or ply:IsSuperAdmin() ) or not args[1] or not args[2] then return end
		
		table.insert( AIDS.Bans, { args[1], args[2] } )
		
		WriteToFile()
		Synch()
	
	end
	concommand.Add( "aids_add_ban", AddBan )
	
end